﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.IO;

namespace ACRE.Tab_Classes
{
    class Hash
    {
        public static void Hash_Text(RadioButton en_radio, RadioButton de_radio, TextBox textBox1, TextBox textBox2, Label name_exist, GroupBox groupBox)
        {
            try
            {
                if (en_radio.Checked)
                {
                    textBox2.Text = RappelzNameCipher.Encrypt(textBox1.Text);

                    string connect = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};", OPT.GetString("db.server"), OPT.GetString("db.name"), OPT.GetString("db.account"), SFM.GetDecryptMD5(OPT.GetString("db.password")));
                    SqlConnection sqlcon = new SqlConnection(connect);
                    sqlcon.Open();
                    SqlDataReader name_check = new SqlCommand("SELECT COUNT(*) FROM [dbo].[data000_idx] where name = '" + textBox1.Text + "'", sqlcon).ExecuteReader();
                    name_check.Read();
                    if (int.Parse(name_check[0].ToString()) == 0)
                    {
                        name_exist.Text = "File Does Not Exist";
                        name_exist.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        name_exist.Text = "File Exist";
                        name_exist.ForeColor = System.Drawing.Color.Green;
                    }
                    name_exist.Location = new System.Drawing.Point(groupBox.Width - name_exist.Width - 6, 73);
                    name_check.Close();
                    sqlcon.Close();
                    sqlcon.Dispose();
                }
                else if (de_radio.Checked)
                {
                    textBox2.Text = RappelzNameCipher.Decrypt(textBox1.Text);

                    string connect = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};", OPT.GetString("db.server"), OPT.GetString("db.name"), OPT.GetString("db.account"), SFM.GetDecryptMD5(OPT.GetString("db.password")));
                    SqlConnection sqlcon = new SqlConnection(connect);
                    sqlcon.Open();
                    SqlDataReader name_check = new SqlCommand("SELECT COUNT(*) FROM [dbo].[data000_idx] where hash = '" + textBox1.Text.Replace("'", "''") + "'", sqlcon).ExecuteReader();
                    name_check.Read();
                    if (int.Parse(name_check[0].ToString()) == 0)
                    {
                        name_exist.Text = "File Does Not Exist";
                        name_exist.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        name_exist.Text = "File Exist";
                        name_exist.ForeColor = System.Drawing.Color.Green;
                    }
                    name_exist.Location = new System.Drawing.Point(groupBox.Width - name_exist.Width - 6, 73);
                    name_check.Close();
                    sqlcon.Close();
                    sqlcon.Dispose();
                }
                if (textBox1.Text == "")
                    name_exist.Text = "";
            }
            catch { }
        }

        public static void Hash_Folder( ProgressBar pBar, Label folder_dir, RadioButton en_folder_radio, RadioButton de_folder_radio, CheckBox ascii_checkbox)
        {
            int file_count = Directory.GetFiles(folder_dir.Text).Length;
            pBar.Value = 0;
            pBar.Maximum = file_count;
            if (en_folder_radio.Checked)
            {
                pBar.Value = 0;
                if (ascii_checkbox.Checked)
                {
                    DirectoryInfo d = new DirectoryInfo(folder_dir.Text);
                    FileInfo[] file = d.GetFiles();
                    foreach (FileInfo f in file)
                    {
                        string ascii_name = Path.GetFileNameWithoutExtension(Path.Combine(f.Directory.ToString(), f.Name)) + "(ascii)" + Path.GetExtension(Path.Combine(f.Directory.ToString(), f.Name));
                        File.Move(f.FullName, Path.Combine(f.Directory.ToString(), RappelzNameCipher.Encrypt(ascii_name)));
                        pBar.Value++;
                    }
                }
                else
                {
                    DirectoryInfo d = new DirectoryInfo(folder_dir.Text);
                    FileInfo[] file = d.GetFiles();
                    foreach (FileInfo f in file)
                    {
                        File.Move(f.FullName, Path.Combine(f.Directory.ToString(), RappelzNameCipher.Encrypt(f.Name)));
                        pBar.Value++;
                    }
                }            
            }
            else if (de_folder_radio.Checked)
            {
                pBar.Value = 0;
                if (ascii_checkbox.Checked)
                {
                    DirectoryInfo d = new DirectoryInfo(folder_dir.Text);
                    FileInfo[] file = d.GetFiles();
                    foreach (FileInfo f in file)
                    {
                        File.Move(f.FullName, Path.Combine(f.Directory.ToString(), RappelzNameCipher.Decrypt(f.Name).Replace("(ascii)", "")));
                        pBar.Value++;
                    }
                }
                else
                {
                    pBar.Value = 0;
                    DirectoryInfo d = new DirectoryInfo(folder_dir.Text);
                    FileInfo[] file = d.GetFiles();
                    foreach (FileInfo f in file)
                    {
                        File.Move(f.FullName, Path.Combine(f.Directory.ToString(), RappelzNameCipher.Decrypt(f.Name)));
                        pBar.Value++;
                    }
                }
            }
            MessageBox.Show("Completed!", "Hash Tool", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }
}
